package org.nobject.common.db.member;

import static org.nobject.common.lang.StringUtils.isEmpty;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.nobject.common.exception.CreateException;
import org.nobject.common.lang.MapUtils;
import org.nobject.common.lang.ObjectUtils;

/**
 * SqlOrder
 *
 * @author bianrongjun
 * @version 1.0
 */
public class SqlOrder {
	
	/** 升序 */
	public static boolean BY_ASC = true;
	/** 降序 */
	public static boolean BY_DESC = false;
	/** 字段 */
	private Map<String, Boolean> orders = null;
	/** 表别名 */
	private String tableName = null;
	
	/** 构造 */
	public SqlOrder() {orders = new LinkedHashMap<String, Boolean>();}

	/** 构造函数 */
	public SqlOrder(Map orders) {this.orders=orders;}
	
	/** 构造函数 */
	public SqlOrder(Object[][] orders) {this.orders=MapUtils.toMap(orders);}
	
	/** 添加 */
	public SqlOrder add(String columnName, boolean order) {orders.put(columnName, order);return this;}
	
	/** 添加降序 */
	public SqlOrder addDesc(String columnName){this.add(columnName,BY_DESC);return this;}
	
	/** 添加升序 */
	public SqlOrder addAsc(String columnName){this.add(columnName,BY_ASC);return this;}
	
	/** 设置表别名 */
	public SqlOrder setTabName(String tabName) {this.tableName = tabName;return this;}
	
	/** 得到orderBy语句 */
	public String getSql() {
		StringBuffer sb = new StringBuffer();
		for (Iterator iterator = this.orders.keySet().iterator(); iterator.hasNext();) {
			if (this.tableName != null) sb.append(this.tableName).append(".");
			String column = (String) iterator.next();
			sb.append(column).append(this.orders.get(column)?" ASC ":" DESC ");
			if (iterator.hasNext()) sb.append(",");
		}
		return sb.toString();
	}
	
	/** 得到orderBy语句 */
	public String getSql(Map<String,String> reflectTabColumns) {
		StringBuffer sb = new StringBuffer();
		for (Iterator iterator = this.orders.keySet().iterator(); iterator.hasNext();) {
			String column = (String) iterator.next();
			if(reflectTabColumns.containsKey(column)) sb.append(reflectTabColumns.get(column)).append(".");
			sb.append(column).append(this.orders.get(column)?" ASC ":" DESC ");
			if (iterator.hasNext()) sb.append(",");
		}
		return sb.toString();
	}
	
	/** 得到orderBy语句 */
	public String getSql(String tableName) {this.tableName=tableName;return getSql();}
	
	/** 判断order元素是否为空 */
	public boolean empty() {return (orders == null || orders.size() == 0);}
	
	/**
	 * 创建
	 * 
	 * @param sqlOrderEles
	 * @param values
	 */
	public static SqlOrder create(SqlOE[] sqlOrderEles, Map<String,String> values) throws CreateException {
		SqlOrder sqlOrder = new SqlOrder();
		if(values==null) return sqlOrder;
		
		
		for (String column : values.keySet()) {

			SqlOE sqlOE = null;
			for (SqlOE soe : sqlOrderEles) {
				if(soe.column.equals(column)){
					sqlOE = soe;
					break;
				}
			}
			
			if (sqlOE == null) throw new CreateException("字段不存在:" + column);
			
			String order = (String)values.get(column);
			if(isEmpty(order)) order="ASC";
			else order=order.toUpperCase();
			
			if(sqlOE!=null){
				if(ObjectUtils.in(order,new String[]{"ASC","DESC"}) ) throw new CreateException("字段排序值无法识别:" + order);
				if(!sqlOE.option.equals(SqlOE.Option.both)){
					if(!order.toLowerCase().equals(sqlOE.option)){
						throw new CreateException("字段排序值不在可用范围:" + order);
					}
				}
			}
			
			sqlOrder.add(column,order.equals("ASC"));
		}
		return sqlOrder;
	}
}
